package fragmentson;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.wq.cycling.R;

import activity.SonActivity;
import fragmentson.backhandle.BackHandledFragment;
import util.dialog.DialogFromMiddle;

/**
 * 报失详情地图页（四种状态）
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class ReportLoss_B extends BackHandledFragment {

	private View rootView;
	private TextureMapView mapView;
	private AMap aMap;
	//mode 0基础地图1卫星地图 road 0路况模式 1非路况模式
	private int mode=0,road=0;
	private GeocodeSearch geocoderSearch;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_reportloss_b, null);
		/*初始化地图和amap对象*/
		mapView = (TextureMapView) rootView.findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 此方法必须重写
		if (aMap == null) {
			aMap = mapView.getMap();
		}
		initView();
		initMapView();
		return rootView;
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.lose_message);
		rootView.findViewById(R.id.iv_mapmode).setOnClickListener(listener);
		rootView.findViewById(R.id.ll_road).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_plus).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_reduce).setOnClickListener(listener);
		rootView.findViewById(R.id.tv_result).setOnClickListener(listener);
		rootView.findViewById(R.id.tv_more).setOnClickListener(listener);

	}

	private void initMapView(){
		//隐藏缩放组件
		aMap.getUiSettings().setZoomControlsEnabled(false);
		//比例尺
		//aMap.getUiSettings().setScaleControlsEnabled(true);
		//logo位置
		aMap.getUiSettings().setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);
		//如果想改变地图中心点
		aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(39.90403, 116.407525),16,0,0)));
		//加载定位maker
		initMarker("39.90403","116.407525");
		initGeocoder();
		getAddress("39.90403","116.407525");
	}

	public void initMarker(String lat,String lng){
		MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
				.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.icon_sos_marker)))
				.position(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)))
				.anchor(0.5f,0.5f)
				.draggable(true);
		aMap.addMarker(markerOption);

	}

	public void getAddress(String latitude,String longitude){
		LatLonPoint latLonPoint = new LatLonPoint(Double.parseDouble(latitude), Double.parseDouble(longitude));
		RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);// 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
		geocoderSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求
	}

	/**
	 * 初始化逆地理编码组件
	 */
	private void initGeocoder(){
		geocoderSearch = new GeocodeSearch(getActivity());
		geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
			@Override
			public void onRegeocodeSearched(RegeocodeResult result, int rCode) {

				if (rCode == AMapException.CODE_AMAP_SUCCESS) {
					if (result != null && result.getRegeocodeAddress() != null
							&& result.getRegeocodeAddress().getFormatAddress() != null) {
						Toast.makeText(getActivity(),result.getRegeocodeAddress().getFormatAddress(),Toast.LENGTH_LONG).show();;

					} else {
						Toast.makeText(getActivity(),"无数据",Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(getActivity(),rCode,Toast.LENGTH_LONG).show();
				}
			}

			@Override
			public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

			}
		});
	}

	public static ReportLoss_B newInstance() {
		ReportLoss_B fragment = new ReportLoss_B();
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.iv_mapmode:
					if(mode==0) {
						((ImageView) v).setImageResource(R.mipmap.icon_right_map3);
						aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
						mode = 1;
					}else{
						((ImageView) v).setImageResource(R.mipmap.icon_right_map2);
						aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
						mode =0;
					}
					break;
				case R.id.ll_road:
					if(road==0) {
						((ImageView) rootView.findViewById(R.id.iv_road)).setImageResource(R.mipmap.icon_right_map5);
						aMap.setTrafficEnabled(true);
						road = 1;
					}else{
						((ImageView) rootView.findViewById(R.id.iv_road)).setImageResource(R.mipmap.icon_right_map4);
						aMap.setTrafficEnabled(false);
						road = 0;
					}
					break;
				case R.id.iv_plus:
					aMap.moveCamera(CameraUpdateFactory.zoomIn());
					break;
				case R.id.iv_reduce:
					aMap.moveCamera(CameraUpdateFactory.zoomOut());
					break;
				case R.id.tv_result:
					DialogFromMiddle dialogFormMiddle = new DialogFromMiddle(getActivity());
					dialogFormMiddle.setSureListener(new DialogFromMiddle.Sure() {
						@Override
						public void onSure() {
							// TODO Auto-generated method stub
						}
					});
					dialogFormMiddle.show(getResources().getString(R.string.dialog_title3), getResources().getString(R.string.dialog_content3), "", "", getResources().getString(R.string.dialog_yes3),getResources().getString(R.string.dialog_no3), true);
					break;
				case R.id.tv_more:
					((SonActivity) getActivity()).addFragment(ReportLoss_G.newInstance());
					break;
				default:
					break;
			}
		}
	};

	@Override
	public boolean onBackPressed() {
		return false;
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onPause() {
		super.onPause();
		mapView.onPause();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}
}
